package payPakage.aliPayPing;

import android.app.Activity;
import android.text.TextUtils;
import android.util.Log;

import com.alipay.sdk.app.PayTask;

import java.util.Map;

import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.PluginRegistry;

public class AliPayPlugin implements MethodChannel.MethodCallHandler {
    public static final String CHANNEL = "com.ahj/aliPayPlugin";
    static MethodChannel channe;
    private Activity activity;
    public static MethodChannel.Result result;

    public AliPayPlugin(Activity activity) {
        this.activity = activity;
    }

    public static void registerWith(FlutterEngine flutterEngine, Activity activity12) {
        channe = new MethodChannel(flutterEngine.getDartExecutor(), CHANNEL);
        AliPayPlugin plugin = new AliPayPlugin(activity12);
        channe.setMethodCallHandler(plugin);
    }


    @Override
    public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {
        try {
            if (methodCall.method.equals("aliPay")) {
                String sign = methodCall.argument("sign");
                Log.e("\"sign:::\"", "sign:::   " + sign);
                startAliPay(sign, result);
            }

        } catch (Exception e) {
            e.printStackTrace();
            result.success("");
        }
    }

    /**
     * 支付宝SDK支付
     *
     * @param alipayinfo
     */
    public void startAliPay(String alipayinfo, MethodChannel.Result result1) {
        if (TextUtils.isEmpty(alipayinfo)) {
            return;
        }
        final Runnable payRunnable = new Runnable() {
            @Override
            public void run() {
                PayTask alipay = new PayTask(activity);
                //第二个参数设置为true，将会在调用pay接口的时候直接唤起一个loading
                final String result = alipay.pay(alipayinfo, true);
                QReaderAliPayResult aliPayResult = new QReaderAliPayResult(result);
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        String resultStatus = aliPayResult.getResultStatus();
                        if (TextUtils.equals(resultStatus, "9000")) {
                            result1.success("1");
                            // 订购成功后显示检查订单倒计时,倒计时完成后刷新界面
                        } else if (TextUtils.equals(resultStatus, "8000")) {
                            result1.success("3");
                            // 订购成功后显示检查订单倒计时,倒计时完成后刷新界面
                        } else if (TextUtils.equals(resultStatus, "6001")) {
                            result1.success("2");
                        } else {
                            result1.success("2");
                        }

                    }
                });

            }
        };

        // 必须异步调用
        Thread payThread = new Thread(payRunnable);
        payThread.start();
    }
}
